//
//  XYSegementView.m
//  XYAuto
//
//  Created by 小萌 on 2016/12/26.
//  Copyright © 2016年 Raymond~. All rights reserved.
//

#import "XYSegementView.h"

@interface XYSegementView()<UIScrollViewDelegate,XYSegementScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *bigScrollView;//底部scrollView
@property (nonatomic, strong) NSMutableArray *topBtnArray;//存放顶部btn
@property (nonatomic, strong) NSMutableArray *contentViewArray;//存放内容
@property (nonatomic, assign) CGFloat dragContentOffX;
@property (nonatomic, assign) BOOL isClickBtn;//是否是点击btn操作
@end

@implementation XYSegementView
#pragma mark - 初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setInit];
    }
    return self;
}
+ (instancetype)segementViewWithFrame:(CGRect)frame
{
    XYSegementView *segementView = [[XYSegementView alloc] initWithFrame:frame];
    return segementView;
}
- (void)setInit
{
    self.topScrollViewHeight = 40.f;
    self.seletedIndex = 0;
    self.contentInset = UIEdgeInsetsMake(self.topScrollViewHeight, 0, 0, 0);
    self.isHideTopSegement = NO;
    self.isScrollContent = YES;
    self.isClickBtn = YES;
    [self addSubview:self.smallScrollView];
    [self addSubview:self.bigScrollView];
//
    self.backgroundColor = [UIColor whiteColor];
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, ScreenHeight)];
    line.backgroundColor =[UIColor clearColor];
    [self addSubview:line];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _smallScrollView.frame = CGRectMake(0, 0, self.bounds.size.width, self.topScrollViewHeight);
    _bigScrollView.frame = CGRectMake(0, self.topScrollViewHeight, self.bounds.size.width,self.bounds.size.height -  self.topScrollViewHeight);
}
#pragma mark - setter && getter
- (XYSegementScrollView *)smallScrollView
{
    if (!_smallScrollView) {
        _smallScrollView = [[XYSegementScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.topScrollViewHeight)];
        _smallScrollView.delegate = self;
        _smallScrollView.segementeDelegate = self;
    }
    return _smallScrollView;
}
- (UIScrollView *)bigScrollView
{
    if (!_bigScrollView) {
        _bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.topScrollViewHeight, self.bounds.size.width,self.bounds.size.height -  self.topScrollViewHeight)];
        _bigScrollView.showsVerticalScrollIndicator = NO;
        _bigScrollView.showsHorizontalScrollIndicator = NO;
        _bigScrollView.pagingEnabled = YES;
        _bigScrollView.delegate = self;
        _bigScrollView.bounces = NO;
        _bigScrollView.autoresizingMask =UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
        _bigScrollView.translatesAutoresizingMaskIntoConstraints = YES;
        _bigScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _bigScrollView;
}
- (NSMutableArray *)topBtnArray
{
    if (!_topBtnArray) {
        _topBtnArray = [NSMutableArray array];
    }
    return _topBtnArray;
}
- (NSMutableArray *)contentViewArray
{
    if (!_contentViewArray) {
        _contentViewArray = [NSMutableArray array];
    }
    return _contentViewArray;
}
- (void)setTopScrollViewHeight:(CGFloat)topScrollViewHeight
{
    if (_topScrollViewHeight != topScrollViewHeight) {
        _topScrollViewHeight = topScrollViewHeight;
    }
    self.smallScrollView.height = _topScrollViewHeight;
    self.contentInset = UIEdgeInsetsMake(_topScrollViewHeight, 0, 0, 0);
}
- (void)setContentInset:(UIEdgeInsets)contentInset {
    _contentInset = contentInset;
   self.bigScrollView.frame = CGRectMake(0, contentInset.top, self.bounds.size.width,self.bounds.size.height -  contentInset.top - contentInset.bottom);
    if (self.contentViewArray.count > 0) {
        for (UIView * view in self.bigScrollView.subviews) {
            [view removeFromSuperview];
        }
        for (UIViewController *vc in self.parentVC.childViewControllers) {
            [vc removeFromParentViewController];
        }
        [self setContentViewController];
        self.bigScrollView.contentSize = CGSizeMake(self.bounds.size.width * self.contentViewArray.count, self.bigScrollView.frame.size.height);
    }
}
- (void)setSeletedIndex:(NSInteger)seletedIndex
{
    if (_seletedIndex != seletedIndex) {
        _seletedIndex = seletedIndex;
    }
    if (_seletedIndex < 0) {
        _seletedIndex = 0;
    }
    if (self.topBtnArray.count > 0) {
        self.isClickBtn = YES;
        UIButton *btn = (UIButton *)[self.smallScrollView viewWithTag:seletedIndex + 500];
        [self.smallScrollView clickBtn:btn];
    }
    self.smallScrollView.seletedIndex = _seletedIndex;
}
- (void)setIsHideTopSegement:(BOOL)isHideTopSegement
{
    if (_isHideTopSegement != isHideTopSegement) {
        _isHideTopSegement = isHideTopSegement;
    }
    self.smallScrollView.hidden = _isHideTopSegement;
}
- (void)setIsScrollContent:(BOOL)isScrollContent
{
    _isScrollContent = isScrollContent;
    if (isScrollContent) {
        self.bigScrollView.scrollEnabled = YES;
    }else{
        self.bigScrollView.scrollEnabled = NO;
    }
}
#pragma mark - reload
- (void)reloadSegementView
{

    NSInteger row = [self.dataSource numberOfRowsInXYSegementView:self];
    self.bigScrollView.contentSize = CGSizeMake(self.bounds.size.width * row, self.bigScrollView.frame.size.height);
    if (self.seletedIndex >= row) {
        self.seletedIndex = row - 1;
    }
    for (NSInteger i = 0; i < row; i ++) {
        UIViewController *vc = [self.dataSource segementView:self rowAtIndexPath:i];
        [self.topBtnArray addObject:vc.title];
        [self.contentViewArray addObject:vc];
    }
    self.smallScrollView.segementArray = self.topBtnArray;
    [self.smallScrollView reloadScrollView];
    [self setContentViewController];
    if ([self.delegate respondsToSelector:@selector(segementView:didSelectRowAtIndexPath:)]) {
        [self.delegate segementView:self didSelectRowAtIndexPath:self.seletedIndex];
    }
    
}
- (void)setContentViewController
{
    [self lazyViewController:self.seletedIndex];
    //滚动到选中的视图
    [self.bigScrollView setContentOffset:CGPointMake(self.seletedIndex * self.bounds.size.width, 0) animated:NO];
}
#pragma mark -UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView == self.bigScrollView) {
        self.dragContentOffX = scrollView.contentOffset.x;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.isClickBtn) {
        [self.smallScrollView selectedTitleBtnColorGradualChangeScrollView:self.smallScrollView];
        return;
    }
    [self.smallScrollView selectedTitleBtnColorGradualChangeScrollView:self.bigScrollView];
    if (scrollView == self.bigScrollView) {
        CGFloat x = scrollView.contentOffset.x - self.dragContentOffX;
        int index = (int)scrollView.contentOffset.x/self.bounds.size.width;
        
        UIButton *button = (UIButton *)[self viewWithTag:index + 500];
        NSInteger rightIndex = index + 1;
        if (rightIndex == self.topBtnArray.count) {
            rightIndex = self.topBtnArray.count - 1;
        }
        UIButton *nextButton = (UIButton *)[self viewWithTag:rightIndex + 500];
        CGFloat centerMargin = nextButton.center.x - button.center.x;
        self.smallScrollView.lbTopLine.originX += (x / self.bounds.size.width * centerMargin) ;
        [self lazyViewController:rightIndex];
        [self lazyViewController:index];
        self.dragContentOffX = scrollView.contentOffset.x;
    }

}
//滚动视图释放滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.bigScrollView) {
        int tag = (int)scrollView.contentOffset.x/self.bounds.size.width + 500;
        UIButton *button = (UIButton *)[self.smallScrollView viewWithTag:tag];
        [self.smallScrollView clickBtn:button];
    }
}
- (void)didSeletedSegementScrollView:(XYSegementScrollView *)view btn:(XYButton *)btn  index:(NSInteger)index
{
    self.seletedIndex = index;
    [self lazyViewController:index];
    if (self.isClickBtn) {
        [self.bigScrollView setContentOffset:CGPointMake(index * self.bounds.size.width, 0) animated:YES];
    }
    if ([self.delegate respondsToSelector:@selector(segementView:didSelectRowAtIndexPath:)]) {
        [self.delegate segementView:self didSelectRowAtIndexPath:self.seletedIndex];
    }
}
- (void)lazyViewController:(NSInteger)index
{
    if (![self.parentVC.childViewControllers containsObject:self.contentViewArray[index]]) {
        UIViewController *vc = self.contentViewArray[index];
        vc.view.frame = CGRectMake(self.contentInset.left + self.bigScrollView.bounds.size.width * index, 0,self.bigScrollView.bounds.size.width - self.contentInset.left - self.contentInset.right, self.bigScrollView.frame.size.height - self.contentInset.bottom - self.contentInset.top);
        [self.parentVC addChildViewController:vc];
        [self.bigScrollView addSubview:vc.view];
    }
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint(self.smallScrollView.frame, point)) {
        self.isClickBtn = YES;
    }else{
        self.isClickBtn = NO;
    }
    return [super hitTest:point withEvent:event];
}
@end
